package com.utilmodule;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * @author liuhongyuan 2017/4/28.
 * @description 软键盘
 */

public class KeyBoardUtils {
   /*
    *@author liuhongyuan
    *@date 2017/4/28 12:04
    *@description 显示软键盘
    */
    public static void showSoftKeyboard(Context mContext, View view) {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 强制隐藏软键盘
     *
     * @author liuhongyuan
     * @date 2016-12-5 下午3:03:49
     * @description
     */
    public static void hideSoftKeyboard(Activity mActivity, View view) {
        InputMethodManager imm = (InputMethodManager) mActivity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); // 强制隐藏键盘
    }

    /**
     * 强制隐藏软键盘
     *
     * @author liuhongyuan
     * @date 2016-12-5 下午3:03:49
     * @description
     */
    public static void hideSoftKeyboard(Activity mActivity) {
        if (mActivity.getCurrentFocus() != null) {
            ((InputMethodManager) mActivity
                    .getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(mActivity.getCurrentFocus()
                                    .getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
